package com.shinhye.elephantsafe.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.shinhye.elephantsafe.db.BlackListOpenHelper;
import com.shinhye.elephantsafe.domain.BlackNumber;

public class BlackListDao {
	private final String TABLE = "blacknumber";
	private BlackListOpenHelper openHelper;

	public BlackListDao(Context context) {
		openHelper = new BlackListOpenHelper(context);
	}
	
	/**
	 * Add a number to blacknumber table.
	 * @param number The number you want to add
	 * @param mode Mode: 0-PHONE, 1-SMS, 2-ALL
	 */
	public void add(String number, int mode) {
		SQLiteDatabase db = openHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("number", number);
		values.put("mode", mode);
		db.insert(TABLE, null, values);
		db.close();
	}
	
	/**
	 * Delete a number from blacknumber table.
	 * @param number The number you want to delete
	 */
	public void delete(String number) {
		SQLiteDatabase db = openHelper.getWritableDatabase();
		db.delete(TABLE, "number=?", new String[]{number});
		db.close();
	}
	
	/**
	 * Update the mode of a number.
	 * @param number The number you want to update
	 * @param mode Mode: 0-PHONE, 1-SMS, 2-ALL
	 */
	public void update(String number, int mode) {
		SQLiteDatabase db = openHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("mode", mode);
		db.update(TABLE, values, "number=?", new String[]{number});
		db.close();
	}
	
	/**
	 * Select the mode of a number.
	 * @param number The number you want to select
	 * @return Mode: 0-PHONE, 1-SMS, 2-ALL; if -1 the number not exist
	 */
	public int find(String number) {
		SQLiteDatabase db = openHelper.getWritableDatabase();
		int mode = -1;
		Cursor cursor = db.query(TABLE, new String[]{"mode"}, "number=?", new String[]{number}, null, null, null);
		if(cursor.moveToNext()) {
			mode = cursor.getInt(0);
		}
		cursor.close();
		db.close();
		return mode;
	}
	
	/**
	 * Find all number in the blacknumber.
	 * @return a list with all black number.
	 */
	public List<BlackNumber> findAll() {
		List<BlackNumber> list = new ArrayList<BlackNumber>();
		SQLiteDatabase db = openHelper.getWritableDatabase();
		
		Cursor cursor = db.query(TABLE, new String[]{"number", "mode"}, null, null, null, null, null);
		while(cursor.moveToNext()) {
			String number = cursor.getString(0);
			int mode = cursor.getInt(1);
			BlackNumber blackNumber = new BlackNumber();
			blackNumber.setNumber(number);
			blackNumber.setMode(mode);
			list.add(blackNumber);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	/**
	 * Find a part of data from the blacknumber table.
	 * @return a list with part of black numbers.
	 */
	public List<BlackNumber> findPart(String start) {
		List<BlackNumber> list = new ArrayList<BlackNumber>();
		SQLiteDatabase db = openHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select number,mode from " + TABLE + " order by _id desc limit 20 offset ?", new String[]{start});
		while(cursor.moveToNext()) {
			String number = cursor.getString(0);
			int mode = cursor.getInt(1);
			BlackNumber blackNumber = new BlackNumber();
			blackNumber.setNumber(number);
			blackNumber.setMode(mode);
			list.add(blackNumber);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	/**
	 * Delete all black numbers in the blacknumber.
	 */
	public void deleteAll() {
		SQLiteDatabase db = openHelper.getWritableDatabase();
		db.delete(TABLE, null, null);
		db.close();
	}
	
	/**
	 * Get the record number of table blacknumber.
	 * @return The count of blacknumber table.
	 */
	public int getCount() {
		SQLiteDatabase db = openHelper.getWritableDatabase();
		int count = 0;
		Cursor cursor = db.rawQuery("select count(*) from " + TABLE, null);
		if(cursor.moveToNext()) {
			count = cursor.getInt(0);
		}
		db.close();
		
		return count;
	}
}
